package com.carbeauty.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.carbeauty.image.ImageSearch;
import com.carbeauty.util.FileUtil;

public class ImageSearchController extends MultiActionController{
	
	public ModelAndView search(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
		ModelAndView Mav = new ModelAndView();
		String pcomtype = request.getParameter("compareType");
		 String fileName = request.getParameter("fileName");
		String _fileScanName = request.getSession().getAttribute("__fileScanName")+"";
		
		if(_fileScanName!=null && !"".equals(_fileScanName)&& !"null".equals(_fileScanName))
		{
			fileName = _fileScanName;
		}
		String isCompare = request.getParameter("isCompare");
		boolean bisCompare = (isCompare==null || "N".equals(isCompare))?false:true;
		int  compareType =4;
		if(pcomtype!=null)
		{
			compareType = Integer.parseInt(pcomtype);
		}
		if(fileName!=null)
		{
			if(fileName.indexOf(".")==-1)
			{
				
				fileName = fileName+".jpg";
			}
			request.setAttribute("compareType", compareType+"");
			request.setAttribute("fileName", fileName+"");
			ArrayList<String> res = new ImageSearch().searchImage(fileName,compareType, 100,bisCompare).getSearchResult();
			
			Mav.addObject("results", res);
			
		}
		 request.getSession().removeAttribute("__fileScanName");
		Mav.setViewName("pages/imageLoaderTest");
		return Mav;
		
	}
}
